﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Frame;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace VideoFileOrganization
{
    public partial class MainFrm : Plugin
    {

        /// <summary>
        /// 应用按钮事件
        /// </summary>
        /// <param name="index">当前选择的页面索引</param>
        public delegate void ApplyButtonDelegate(int index);
        public event ApplyButtonDelegate ApplyButtonEvent;
        /// <summary>
        /// 设置根目录
        /// </summary>
        /// <param name="path">根目录</param>
        public delegate void SetRootPathDelegate(string path);
        public event SetRootPathDelegate setRootPathEvent;
        /// <summary>
        /// 刷新界面
        /// </summary>
        /// <param name="index">当前选择的页面索引</param>
        public delegate void InitialUiDelegate(int index);
        public event InitialUiDelegate InitialUiEvent;

        Global m_global;
        //窗口说明
        public Organizesubdocuments m_OrgDocFrm;

        public MainFrm(FrameApplication app) : base(app)
        {
            InitializeComponent();
            m_app.InitialUiEvnet += InitUI;
            m_global = new Global();
            this.m_InitShow = true;
            this.m_inToolBar = true; ;//是否显示在工具栏上
            this.Enabled = true;   //窗体是否有效
            this.FloatSize = this.Size; //悬浮显示的窗口大小就是现在的大小
            this.OnMenu = "Documentation";
            this.toolBarIcon = Properties.Resources.Main;
            this.Text = "视频文件整理";
            tsTbx.Text = m_global.Path;
           
        }

        private void SendToLogWindows(string Msg, LogLevel l, Form frm)
        {
            Msg = "[" + frm + "]" + Msg;
            m_app.Log.AddLog(Msg, l);
        }

        public override void InitUI()
        {
            tabPage.TabPages.Clear();
            tabimageList.Images.Clear();
            for(int i=0; i<m_global.SubFormDic.Count; i++)
            {
                AddFrm(m_global.SubFormDic[i]);
            }
            if (!string.IsNullOrEmpty(tsTbx.Text))
                SetRootPath(tsTbx.Text);
        }


        private void tsBtnScan_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = "选择文件所在文件夹目录";
            string selectPath = tsTbx.Text;
            if (selectPath != string.Empty)
            {
                if (Directory.Exists(selectPath))
                {
                    while (!Directory.Exists(selectPath))
                    {
                        selectPath = Directory.GetParent(selectPath).FullName;
                    }
                    folderBrowserDialog.SelectedPath = selectPath;
                }
                else
                {
                    folderBrowserDialog.SelectedPath = "";
                }
            }
            if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (string.IsNullOrEmpty(folderBrowserDialog.SelectedPath))
                {
                    MessageBox.Show(this, "文件夹路径不能为空", "信息提示");
                    return;
                }
                SetRootPath(folderBrowserDialog.SelectedPath);
                m_global.SaveHistory();
            }
        }

        private void SetRootPath(string path)
        {
            tsTbx.Text = path;
            m_global.Path = path;
            setRootPathEvent(path);
        }

        private void AddFrm(BaseFrm frm)
        {
            frm.TopLevel = false;
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            frm.sendMessageEvent += new BaseFrm.SenMessageEventHanle(SendToLogWindows);
            this.ApplyButtonEvent += new ApplyButtonDelegate(frm.OnApplyButton);
            this.setRootPathEvent += new SetRootPathDelegate(frm.SetRootPath);
            this.InitialUiEvent += new InitialUiDelegate(frm.UpdataUi);
            frm.global = m_global;
            tabPage.TabPages.Add(frm.Text);
            frm.Parent = tabPage.TabPages[tabPage.TabPages.Count - 1];
            tabimageList.Images.Add(frm.TabImage);
            tabPage.ImageList = tabimageList;
            int index = tabimageList.Images.Count - 1;
            tabPage.TabPages[index].ImageIndex = index;
            frm.Show();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            ApplyButtonEvent(tabPage.SelectedIndex);
        }

        private void btnUpdataUi_Click(object sender, EventArgs e)
        {
            InitialUiEvent(tabPage.SelectedIndex);
        }

        private void tabPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitialUiEvent(tabPage.SelectedIndex);
        }

        private void btnOpenDir_Click(object sender, EventArgs e)
        {
            if (tsTbx.Text == string.Empty || !Directory.Exists(tsTbx.Text)) return;
            Process.Start(tsTbx.Text);
        }
    }
}
